package entreprise;

import java.util.ArrayList;

import database.Database;
import mapping.DBClient;

public class Client extends DBClient {
	private Database database;

	public Client() {
		super();
		database = new Database("restoreservation");
	}

	public Client(String name_client, String user_id, String passwd,
			String birthday, String numberphone, String email) {
		super(name_client, user_id, passwd, birthday, numberphone, email);
		database = new Database("restoreservation");
	}

	public Database getDatabase() {
		return database;
	}

	public void setDatabase(Database database) {
		this.database = database;
	}

	public void setDatabase(String db_name) {
		this.database = new Database(db_name);
	}

	public boolean addClient() {
		String sql_str = "";
		int res = 0;
		try {
			sql_str = "INSERT INTO client(name_client, user_id, passwd, birthday, "
					+ " numberphone, email) VALUES('"
					+ this.getName_client()
					+ "', '"
					+ this.getUser_id()
					+ "', '"
					+ this.getPasswd()
					+ "', '"
					+ this.getBirthday()
					+ "', '"
					+ this.getNumberphone() + "', '" + this.getEmail() + "')";
			res = database.set(sql_str);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		if (res != 0)
			return true;
		return false;
	}

	public boolean updateClient() {
		String sql_str = "";
		try {
			sql_str = "UPDATE client SET name_client = '"
					+ this.getName_client() + "', user_id = '"
					+ this.getUser_id() + "', passwd = '" + this.getPasswd()
					+ "', birthday = '" + this.getBirthday()
					+ "', numberphone = '" + this.getNumberphone()
					+ "', email = '" + this.getEmail() + "'";
			database.set(sql_str);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public ArrayList<ArrayList<String>> getAllClient() {
		String str_sql = "select * from client";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		datas = database.get(str_sql);
		return datas;
	}

	public ArrayList<String> listClientWithId(String id_client) {
		String str_sql = "select * from client where id_client = '" + id_client +  "'";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		ArrayList<String> data = new ArrayList<String>();
		datas = database.get(str_sql);
		if (datas.size() > 0)
			data = datas.get(0);
		return data;
	}
	
	
	public ArrayList<String> listClientWithUserIdPasswd(String user_id,
			String passwd) {
		String str_sql = "select * from client where user_id = '" + user_id + 
				"' and passwd = '" + passwd + "'";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		ArrayList<String> data = new ArrayList<String>();
		datas = database.get(str_sql);
		if (datas.size() > 0)
			data = datas.get(0);
		return data;
	}

	public ArrayList<String> listClientWithUserID(String user_id) {

		String str_sql = "select * from client where user_id = '" + user_id + "'";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		ArrayList<String> data = new ArrayList<String>();
		datas = database.get(str_sql);
		if (datas.size() > 0)
			data = datas.get(0);
		return data;
	}
	
	public String getEmail(String id_client)
	{
		String str_sql = "select email from client where id_client = '" + id_client + "'";
		ArrayList<ArrayList<String>> datas = new ArrayList<ArrayList<String>>();
		String data = null;
		datas = database.get(str_sql);
		if (datas.size() > 0)
			data = datas.get(0).get(0);
		return data;
	}

}
